package com.huiyujinrong.qiangdanbao.activity.fragment;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.adapter.OderAdapter;
import com.huiyujinrong.qiangdanbao.bean.OderBean;
import com.huiyujinrong.qiangdanbao.network.Api;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单
 * 2017/4/11
 * yd
 */
public class FragmentOrder extends Fragment implements FragmentOrderContract.View {

    @BindView(R.id.toolbar_Title)
    TextView tool_title;

    @BindView(R.id.subTitle)
    TextView tvSubTitle;
    private FragmentOrderPresenter presenter;

    private Toast t;

    @BindView(R.id.oder_listview)
    ListView oder_listview;

    private OderBean data;

    private List<OderBean.ListBean> listBean;

    private OderAdapter adapter;

    @BindView(R.id.oder_showS)
    RelativeLayout shows;

    @BindView(R.id.oder_pull)
    PullRefreshLayout pull;

    //    @BindView(R.id.zanwei_view)
//    View viewBg;//暂位背景
    private Jump jump;

    private int pullNum = 1;  //页码

    private int status = 0;  //订单状态码

    private ProgressDialog progressDialog;

    private boolean isReflush;

    private View popuView;
    private TextView item_1, item_2, item_3, item_4, item_5;
    private int popuNumber = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_order, container, false);
        ButterKnife.bind(this, v);

        tool_title.setText("订单");
        tvSubTitle.setVisibility(View.VISIBLE);
        tvSubTitle.setText("全部订单");

        popuView = LayoutInflater.from(getActivity()).inflate(R.layout.popu_item, null);
        item_1 = (TextView) popuView.findViewById(R.id.item_1);//全部订单
        item_2 = (TextView) popuView.findViewById(R.id.item_2);//审核中
        item_3 = (TextView) popuView.findViewById(R.id.item_3);//审核通过
        item_4 = (TextView) popuView.findViewById(R.id.item_4);//审核拒绝
        item_5 = (TextView) popuView.findViewById(R.id.item_5);//放款成功
        item_1.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
        return v;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new FragmentOrderPresenter(this, getActivity());
        presenter.start();


        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setCancelable(false);


        jump = (Jump) getActivity();
        listBean = new ArrayList<>();
        adapter = new OderAdapter(listBean, getActivity());
        oder_listview.setAdapter(adapter);


        //刷新
        pull.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pull.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isReflush = true;
                        pullNum = 1;
                        presenter.getOrder(String.valueOf(SharedHelper.get(getActivity(), Api.USER_ID, Api.nul)), pullNum, status);
                        pull.setRefreshing(false);
                    }
                }, 1000);
            }
        });


        //加载  判断是都到列表底部
        oder_listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                isReflush = false;
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (data.isHasNextPage() == true) {
                            ++pullNum;
                            presenter.getOrder(String.valueOf(SharedHelper.get(getActivity(), Api.USER_ID, Api.nul)), pullNum, status);
                        } else {
                            showToast("没有更多数据了");
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        inData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        inData();
    }


    //请求数据
    private void inData() {
        isReflush = true;
        pull.setRefreshing(true);
        presenter.getOrder(String.valueOf(SharedHelper.get(getActivity(), Api.USER_ID, Api.nul)), pullNum, status);
    }


    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }


    /**
     * 全部订单  点击选择
     *
     * @param v
     */
    @OnClick(R.id.subTitle)
    public void onClick(View v) {
        showItem(popuNumber);
        showPopupWindow(v);
        backgroundAlpha(getActivity(), 0.5f);
    }

    /**
     * 全部订单
     *
     * @param view
     */
    private void showPopupWindow(View view) {
        pullNum = 1;
        final PopupWindow popupWindow = new PopupWindow(popuView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(getActivity(), 1f);
            }
        });
        popupWindow.setTouchable(true);
        popupWindow.showAsDropDown(view);

        //全部订单
        item_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                showItem(1);
                status = 0;
                inData();
            }
        });

        //审核中
        item_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                showItem(2);
                status = 2;
                inData();
            }
        });

        //审核通过
        item_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                showItem(3);
                status = 3;
                inData();
            }
        });

        //审核拒绝
        item_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                showItem(4);
                status = 4;
                inData();
            }
        });

        //放款成功
        item_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                showItem(5);
                status = 5;
                inData();
            }
        });


    }

    private void showItem(int itemNumber) {
        item_1.setBackground(getResources().getDrawable(R.drawable.shape_hui_kuang));
        item_2.setBackground(getResources().getDrawable(R.drawable.shape_hui_kuang));
        item_3.setBackground(getResources().getDrawable(R.drawable.shape_hui_kuang));
        item_4.setBackground(getResources().getDrawable(R.drawable.shape_hui_kuang));
        item_5.setBackground(getResources().getDrawable(R.drawable.shape_hui_kuang));
        switch (itemNumber) {
            case 1:
                item_1.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                tvSubTitle.setText("全部订单");
                popuNumber = 1;
                break;
            case 2:
                item_2.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                tvSubTitle.setText("审核中");
                popuNumber = 2;
                break;
            case 3:
                item_3.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                tvSubTitle.setText("审核通过");
                popuNumber = 3;
                break;
            case 4:
                item_4.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                tvSubTitle.setText("审核拒绝");
                popuNumber = 4;
                break;
            case 5:
                item_5.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                tvSubTitle.setText("放款成功");
                popuNumber = 5;
                break;

        }
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    @OnClick(R.id.oder_oneOder)
    void gotoHome() {
        if (jump != null) {
            jump.jump();
        }
    }

    @Override
    public void setData(OderBean bean) {
        data = bean;
        pull.setRefreshing(false);
        if (data.getList().size() < 1) {
            shows.setVisibility(View.VISIBLE);
            pull.setVisibility(View.GONE);
        } else {
            pull.setVisibility(View.VISIBLE);
            shows.setVisibility(View.GONE);
            if (isReflush) {
                listBean.clear();
            }
            listBean.addAll(data.getList());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.end();
    }

    @Override
    public void disPlay() {
        progressDialog.setMessage("正在加载，请稍后");
        progressDialog.show();
    }

    @Override
    public void disMiss() {
        progressDialog.dismiss();
    }

    //提供给activityfragment 的 接口  实现 切换fragment
    public interface Jump {
        void jump();
    }

}
